using System;
using System.Collections.Generic;
using UnityEngine;

public class FriendProfileButton : MonoBehaviour
{
	[Serializable]
	public struct WidgetDepthInfo
	{
		public UIWidget widget;

		public int baseDepth;
	}

	public delegate void OnTextureUpdate(Texture2D texture);

	private OnTextureUpdate onTextureUpdate;

	public List<WidgetDepthInfo> myWidgets = new List<WidgetDepthInfo>();

	public new UILabel name;

	public UITexture texture;

	public float approachError = 0.01f;

	public float lerpSpeed = 1f;

	private List<Vector3> desiredPositions = new List<Vector3>();

	private FriendProfileDisplayController displayController;

	private int totalFriendsAtStage;

	private bool isExpanding;

	private Vector3 unexpandedPosition = Vector3.zero;

	private Vector3 initialExpansionPosition = Vector3.zero;

	private float expansionTimeCounter;

	public float expansionSpeed = 2f;

	public float expansionWaitTime = 5f;

	public string friendId
	{
		get
		{
			if (friendProfile != null)
			{
				return friendProfile.fbIdForApp;
			}
			return string.Empty;
		}
	}

	public FriendProfileConfig.FriendProfileWrapper friendProfile { get; protected set; }

	public int maxStageIndex
	{
		get
		{
			if (friendProfile != null)
			{
				return friendProfile.maxStageReached;
			}
			return 0;
		}
	}

	public Texture2D myTexture { get; protected set; }

	public void Init(FriendProfileConfig.FriendProfileWrapper friendProfile, List<Vector3> desiredPositions, FriendProfileDisplayController.FriendsAtStageCount stageCount, int groupId, FriendProfileDisplayController displayController)
	{
		if (groupId >= 0)
		{
			GGServerRequestsBackend.TextureRequest textureRequest = new GGServerRequestsBackend.TextureRequest(friendProfile.pictureUrl);
			textureRequest.groupId = groupId;
			textureRequest.onComplete = OnTextureFetchComplete;
			textureRequest.cache = CacheStategy.CacheToFile;
			textureRequest.cacheTimeToLive = GGRequestCache.InfiniteTimeToLive();
			BehaviourSingleton<GGRequestLimitter>.instance.Add(textureRequest);
		}
		if (isExpanding)
		{
			this.desiredPositions.Clear();
		}
		isExpanding = false;
		unexpandedPosition = Vector3.zero;
		initialExpansionPosition = Vector3.zero;
		expansionTimeCounter = 0f;
		this.displayController = displayController;
		this.friendProfile = friendProfile;
		AddDesiredPositionPoints(desiredPositions);
		totalFriendsAtStage = stageCount.totalFriendsAtStage;
		foreach (WidgetDepthInfo myWidget in myWidgets)
		{
			myWidget.widget.depth = myWidget.baseDepth + stageCount.currentButtonOffset * myWidgets.Count;
		}
		UITools.ChangeText(name, GGFormat.FirstName(friendProfile.playerName));
		base.gameObject.SetActive(true);
	}

	public void AddDesiredPositionPoints(List<Vector3> desiredPositions)
	{
		desiredPositions.AddRange(this.desiredPositions);
		this.desiredPositions = desiredPositions;
	}

	public void OnTextureFetchComplete(GGServerRequestsBackend.ServerRequest request)
	{
		Debug.Log(friendProfile.playerName);
		if (request.status != 0)
		{
			Debug.LogError("OnTextureFetchComplete" + request.status);
			return;
		}
		myTexture = request.GetResponse<Texture2D>();
		texture.mainTexture = myTexture;
		displayController.OnTextureArrived();
		if (onTextureUpdate != null)
		{
			onTextureUpdate(myTexture);
		}
	}

	private void Update()
	{
		if (isExpanding)
		{
			if (desiredPositions.Count > 0)
			{
				expansionTimeCounter += expansionSpeed * Time.deltaTime;
				base.transform.localPosition = Vector3.Lerp(initialExpansionPosition, desiredPositions[0], expansionTimeCounter);
				if (expansionTimeCounter >= 1f)
				{
					isExpanding = desiredPositions[0] != unexpandedPosition;
					desiredPositions.Clear();
					expansionTimeCounter = 0f;
				}
			}
			else
			{
				expansionTimeCounter += Time.deltaTime;
				if (expansionTimeCounter > expansionWaitTime)
				{
					initialExpansionPosition = base.transform.localPosition;
					desiredPositions.Add(unexpandedPosition);
					expansionTimeCounter = 0f;
				}
			}
		}
		else
		{
			if (desiredPositions.Count == 0)
			{
				return;
			}
			float distanceToLastDesiredPosition = GetDistanceToLastDesiredPosition();
			if (distanceToLastDesiredPosition < approachError)
			{
				base.transform.localPosition = desiredPositions[0];
				desiredPositions.Clear();
				return;
			}
			float num = Mathf.Lerp(0f, distanceToLastDesiredPosition, lerpSpeed * Time.deltaTime);
			Vector3 vector = base.transform.localPosition;
			for (int num2 = desiredPositions.Count - 1; num2 >= 0; num2--)
			{
				Vector3 vector2 = desiredPositions[num2];
				float magnitude = (vector2 - vector).magnitude;
				if (magnitude > num)
				{
					vector += num * (vector2 - vector).normalized;
					num = 0f;
					break;
				}
				num -= magnitude;
				vector = vector2;
				desiredPositions.RemoveAt(num2);
			}
			base.transform.localPosition = vector;
		}
	}

	private float GetDistanceToLastDesiredPosition()
	{
		Vector3 vector = base.transform.localPosition;
		float num = 0f;
		for (int num2 = desiredPositions.Count - 1; num2 >= 0; num2--)
		{
			num += (desiredPositions[num2] - vector).magnitude;
			vector = desiredPositions[num2];
		}
		return num;
	}

	public void SetTextureUpdateListener(OnTextureUpdate onUpdate)
	{
		onTextureUpdate = onUpdate;
	}

	public void OnClickFriendButton()
	{
		if (!isExpanding && totalFriendsAtStage > 1 && desiredPositions.Count == 0)
		{
			displayController.ExpandButtonsAtStage(maxStageIndex);
		}
	}

	public void DoExpandView(Vector3 desiredPosition)
	{
		isExpanding = true;
		unexpandedPosition = base.transform.localPosition;
		initialExpansionPosition = base.transform.localPosition;
		expansionTimeCounter = 0f;
		desiredPositions.Add(desiredPosition);
	}
}
